<template>
    <el-row type="flex" justify="center">
    <el-col :span="16" class="boxdiv">
    <h1 style="color:#fff;">注册</h1>
      <el-form :model="form" ref="form" :rules="rules">
        <el-form-item  prop="username">
          <el-input type="text" placeholder="请输入用户名" v-model="form.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item  prop="password">
          <el-input type="password" placeholder="请输入密码" v-model="form.password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item  prop="spassword">
          <el-input type="password" placeholder="确认密码" v-model="form.spassword" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-row :gutter="10">
            <el-col :span="12"><el-button type="primary" style="width:100%" @click="submitForm('form')">确认注册</el-button></el-col>
            <el-col :span="12"><el-button style="width:100%" @click="showlogin">已有账号去登录</el-button></el-col>
          </el-row> 
        </el-form-item>
      </el-form>
    </el-col>
    </el-row>
</template>

<script>
export default {
  name: 'userlogin',
  data () {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.form.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      form: {
        username: '',
        password: '',
        spassword: ''
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'change' }
        ],
        spassword: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      },
      labelPosition: 'left'
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.H.ajax('/common/login/register', this.form, 'post', (data) => {
            // 登陆成功
            if (data.success) {
              // 注册成功后弹出提示，打开登录页面
              this.$confirm('注册成功！, 是否现在登录?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'success'
              }).then(() => {
                this.$router.push({path: '/userlogin'})
              }).catch(() => {})
            } else {
              this.$message.error(data.info)
            }
          })
        } else {
          return false
        }
      })
    },
    showlogin () {
      this.$router.push({path: '/userlogin'})
    }
  },
  mounted: function () {
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.boxdiv{
  padding:10px 60px 40px;
  margin:0px auto;
  background-color: rgba(2, 1, 2, 0.32);
  border-radius: 8px;
  box-shadow: 0 1px 1px rgba(0,0,0,.15);
  border-color: transparent;
  margin-top:100px;
}
</style>
